package cd.tools_test;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by tecsun on 2018/11/27.
 */

public class NetworkUtil {
    public static boolean isNetworkAvailable(Context context) {
        try {
            ConnectivityManager connectivity = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {

                NetworkInfo info = connectivity.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {

                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public static int getNetworkConnectType(Context context) {
        ConnectivityManager connectMgr = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if(connectMgr == null)
            return -1;

        NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mMobileNetworkInfo = connectMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (mMobileNetworkInfo != null && mMobileNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
            return 0;
        }else if (ethNetInfo != null && ethNetInfo.getState() == NetworkInfo.State.CONNECTED) {
            return 1;
        } else if (wifiNetInfo != null && wifiNetInfo.getState() == NetworkInfo.State.CONNECTED) {
            return 2;
        } else {
            return 3;
        }
    }


    /* @author suncat
     * @category 判断是否能够ping通
     * @return
     */
    public static final boolean ping(String ip) {

        boolean result;
        InputStream input = null;
        BufferedReader in = null;
        try {
            Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping网址3次
            // 读取ping的内容，可以不加
            input = p.getInputStream();
            in = new BufferedReader(new InputStreamReader(input));
            StringBuffer stringBuffer = new StringBuffer();
            String content = "";
            while ((content = in.readLine()) != null) {
                stringBuffer.append(content);
            }
            Log.d("chenda", "result content : " + stringBuffer.toString());
            // ping的状态
            int status = p.waitFor();
            result = status == 0;
        } catch (IOException e) {
            result = false;
            e.printStackTrace();
        } catch (InterruptedException e) {
            result = false;
            e.printStackTrace();
        } finally {
            try {
                if (input != null)
                    input.close();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static boolean isConnByHttp(String addr){
        boolean isConn = false;
        URL url;
        HttpURLConnection conn = null;
        try {
            url = new URL(addr);
            conn = (HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(1000*5);
            if(conn.getResponseCode()==200){
                isConn = true;
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block             e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block             e.printStackTrace();
        }finally{
            conn.disconnect();
        }
        return isConn;
    }

}
